//#define ZDEBUG

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;


namespace ZombieGame
{

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class ZombieGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static SpriteBatch TextDrawer;
        public World World;
        public static ZombieGame Current;
        public Player Player;
        public static Random Random;
        public static SpriteFont Font1;
        public static Texture2D damage;
        public Vector2 MousePosition { get { return new Vector2(InputHandler.CurrentMouseState.X, InputHandler.CurrentMouseState.Y) / Camera.Scale + Camera.Position; } }
        public Vector2 Center { get { return PointToVector2(GraphicsDevice.Viewport.Bounds.Center); } }
        public Menu Menu;
        Point screenSize;
        public static Stopwatch Playtime;

        public ZombieGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Current = this;
            IsMouseVisible = true;
            IsFixedTimeStep = false;
            Window.AllowUserResizing = true;
            graphics.SynchronizeWithVerticalRetrace = false;
            if (Settings.Default.defWidth == 0)
                screenSize.X = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            else
                screenSize.X = Settings.Default.defWidth;
            if (Settings.Default.defHeight == 0)
                screenSize.Y = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            else
                screenSize.Y = Settings.Default.defHeight;
            graphics.IsFullScreen = Settings.Default.fullscreen;
            Random = new Random();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            //World = World.LoadWorld(@"C:\Users\Bar\Pictures\ZombieGame\testmapcomplete.puw");
            //Player = new Player();
            //Player.Position = World.PSpawners[0];
            //World.Players.Add(Player);
            Camera.Rectangle = new Rectangle(0, 0, 1280, 720);
            Camera.Enabled = false;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            TextDrawer = new SpriteBatch(GraphicsDevice);
            Font1 = Content.Load<SpriteFont>("Font1");
            damage = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            damage.SetData<Color>(new Color[] { Color.Red });
            Menu.LoadMenus();
            Menu = Menu.MainMenu;
            Playtime = new Stopwatch();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            if (Menu != null)
                Menu.Update(gameTime);
            if (World != null)
                World.Update(gameTime);
            InputHandler.Update();
            Current = this;
            if (Camera.Enabled)
            {
                if (Camera.Position.X < 0)
                    Camera.Position = new Vector2(0f, Camera.Position.Y);
                if (Camera.Position.Y < 0)
                    Camera.Position = new Vector2(Camera.Position.X, 0f);
                if (Camera.Rectangle.Right > World.Size.X)
                    Camera.Position = new Vector2(World.Size.X - Camera.Rectangle.Width, Camera.Position.Y);
                if (Camera.Rectangle.Bottom > World.Size.Y)
                    Camera.Position = new Vector2(Camera.Position.X, World.Size.Y - Camera.Rectangle.Height);
            }
            if (((InputHandler.IsNewDown(Keys.LeftAlt) || InputHandler.IsNewDown(Keys.RightAlt)) && InputHandler.IsDown(Keys.Enter))
                || (InputHandler.IsNewDown(Keys.Enter) && (InputHandler.IsDown(Keys.LeftAlt) || InputHandler.IsDown(Keys.RightAlt))))
            {
                graphics.PreferredBackBufferHeight = screenSize.Y;
                graphics.PreferredBackBufferWidth = screenSize.X;
                graphics.ToggleFullScreen();
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteSortMode.BackToFront, null, null, null, null, null, Camera.Matrix);
            TextDrawer.Begin(SpriteSortMode.BackToFront, null);
            if (Player != null)
                spriteBatch.Draw(damage, Camera.Rectangle, null, Color.White * (0.5f - (Player.Health / (Player.MaxHealth * 2))));
            if (Menu != null)
                Menu.Draw(TextDrawer);
            if (World != null)
                World.Draw(spriteBatch);
            spriteBatch.End();
            TextDrawer.End();
            base.Draw(gameTime);
        }

        public static Vector2 PointToVector2(Point p)
        {
            return new Vector2(p.X, p.Y);
        }
    }
}
